#include "stdafx.h"
#include "Object.h"

extern objects object[10];
extern int level;

#include "Texture.h"
extern TextureImage textures[10];

int Compare(objects *elem1, objects *elem2)		// Compare Function *** MSDN CODE MODIFIED FOR THIS TUT ***
{
   if ( elem1->distance < elem2->distance)						// If First Structure distance Is Less Than The Second
      return -1;												// Return -1
   else if (elem1->distance > elem2->distance)					// If First Structure distance Is Greater Than The Second
      return 1;													// Return 1
   else															// Otherwise (If The distance Is Equal)
      return 0;													// Return 0
}

GLvoid objects::Init()										// Initialize An Object
{
	rot=1;											// Clockwise Rotation
	frame=0;										// Reset The Explosion Frame To Zero
	hit=FALSE;										// Reset Object Has Been Hit Status To False
	texid=rand()%5;									// Assign A New Texture
	distance=-(float(rand()%4001)/100.0f);			// Random Distance
	y=-1.5f+(float(rand()%451)/100.0f);				// Random Y Position
	// Random Starting X Position Based On Distance Of Object And Random Amount For A Delay (Positive Value)
	x=((distance-15.0f)/2.0f)-(5*level)-float(rand()%(5*level));
	dir=(rand()%2);									// Pick A Random Direction

	if (dir==0)										// Is Random Direction Right
	{
		rot=2;										// Counter Clockwise Rotation
		x=-x;							// Start On The Left Side (Negative Value)
	}

	if (texid==0)									// Blue Face
		y=-2.0f;									// Always Rolling On The Ground

	if (texid==1)									// Bucket
	{
		dir=3;										// Falling Down
		x=float(rand()%int(distance-10.0f))+((distance-10.0f)/2.0f);
		y=4.5f;										// Random X, Start At Top Of The Screen
	}

	if (texid==2)									// Target
	{
		dir=2;										// Start Off Flying Up
		x=float(rand()%int(distance-10.0f))+((distance-10.0f)/2.0f);
		y=-3.0f-float(rand()%(5*level));			// Random X, Start Under Ground + Random Value
	}

	// Sort Objects By Distance:	Beginning Address Of Our object Array	*** MSDN CODE MODIFIED FOR THIS TUT ***
	//								Number Of Elements To Sort
	//								Size Of Each Element
	//								Pointer To Our Compare Function
	qsort((void *) &object, level, sizeof(objects), (compfn)Compare );
}

extern int miss;

void objects::Update(DWORD ms)
{
	/*
	if (rot==1)								// If Rotation Is Clockwise
		spin-=0.2f*(float(loop+ms));	// Spin Clockwise

	if (rot==2)								// If Rotation Is Counter Clockwise
		spin+=0.2f*(float(loop+ms));	// Spin Counter Clockwise
		*/
	if (dir==1)								// If Direction Is Right
		x+=0.012f*float(ms);			// Move Right

	if (dir==0)								// If Direction Is Left
		x-=0.012f*float(ms);			// Move Left

	if (dir==2)								// If Direction Is Up
		y+=0.012f*float(ms);			// Move Up

	if (dir==3)								// If Direction Is Down
		y-=0.0025f*float(ms);		// Move Down

	// If We Are To Far Left, Direction Is Left And The Object Was Not Hit
	if ((x<(distance-15.0f)/2.0f) && (dir==0) && !hit)
	{
		miss+=1;											// Increase miss (Missed Object)
		hit=TRUE;								// Set hit To True To Manually Blow Up The Object
	}

	// If We Are To Far Right, Direction Is Left And The Object Was Not Hit
	if ((x>-(distance-15.0f)/2.0f) && (dir==1) && !hit)
	{
		miss+=1;											// Increase miss (Missed Object)
		hit=TRUE;								// Set hit To True To Manually Blow Up The Object
	}

	// If We Are To Far Down, Direction Is Down And The Object Was Not Hit
	if ((y<-2.0f) && (dir==3) && !hit)
	{
		miss+=1;											// Increase miss (Missed Object)
		hit=TRUE;								// Set hit To True To Manually Blow Up The Object
	}

	if ((y>4.5f) && (dir==2))		// If We Are To Far Up And The Direction Is Up
		dir=3;									// Change The Direction To Down
}

void objects::Draw(float width,float height)				// Draw Object Using Requested Width, Height And Texture
{
	glBindTexture(GL_TEXTURE_2D, textures[texid].texID);		// Select The Correct Texture
	glBegin(GL_QUADS);											// Start Drawing A Quad
		glTexCoord2f(0.0f,0.0f); glVertex3f(-width,-height,0.0f);	// Bottom Left
		glTexCoord2f(1.0f,0.0f); glVertex3f( width,-height,0.0f);	// Bottom Right
		glTexCoord2f(1.0f,1.0f); glVertex3f( width, height,0.0f);	// Top Right
		glTexCoord2f(0.0f,1.0f); glVertex3f(-width, height,0.0f);	// Top Left
	glEnd();													// Done Drawing Quad
}
